package com.danbay.cloud.lock.device.service;

import com.danbay.cloud.lock.device.api.dto.DataSyncReportDto;
import com.danbay.cloud.lock.device.entity.LockPrePassword;
import com.danbay.framework.redis.RedisClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author yangyanchen on 2017/12/19
 *         Copyright © 2016 －2017 旦倍科技
 */
@Service
public class DeviceInfoCacheService {

    private final RedisClient redisClient;
    private final String DATA_SYNC_KEY = "data_sync_";
    private final String LOCK_PRE_PASSWORD_KEY = "lock_pre_password_key_";

    @Autowired
    public DeviceInfoCacheService(RedisClient redisClient) {
        this.redisClient = redisClient;
    }

    public void setDataSync(String deviceId, DataSyncReportDto dataSyncReportDto){
        redisClient.setEntity(DATA_SYNC_KEY+deviceId,dataSyncReportDto,60 * 5);
    }

    public void setLockPrePassword(String deviceId, LockPrePassword lockPrePassword){
        redisClient.setEntity(generateKey(deviceId,
                lockPrePassword.getPsw_alias(),lockPrePassword.getPsw_type()),lockPrePassword);
    }

    public LockPrePassword getLockPrePassword(String deviceId,String alias,String pwdType){
        return redisClient.getEntity(generateKey(deviceId,alias,pwdType));
    }

    private String generateKey(String deviceId,String alias,String pwdType){
        StringBuilder builder = new StringBuilder();
        builder.append(LOCK_PRE_PASSWORD_KEY)
                .append(deviceId)
                .append("_")
                .append(alias)
                .append("_")
                .append(pwdType);
        return builder.toString();
    }

}
